import service from '@/service';

export default {
    state: {
        conf: {},
        info: {},
        token: uni.getStorageSync('token') || '',
        app: uni.getStorageSync('app') || '5gqy',
        sdkType: uni.getStorageSync('sdkType') || 'zfb',
        back: uni.getStorageSync('back') || ''
    },

    actions: {
        getUserInfo({ commit, state }) {
            return new Promise((resolve, reject) => {
                service.million.bwbtUser
                    .getUserInfo({
                        sdkType: state.sdkType,
						isBybt: true
                    })
                    .then(res => {
                        commit('SET_USERINFO', res.user);
                        commit('SET_CONF', res.config);
                        resolve(res);
                    })
                    .catch(err => {
                        reject(err);
                    });
            });
        },
        addVisitRecordZfb({ commit }, state) {
			let params = {
				pageType: state.pageType
			}
			if (state.floorId) {
				params.floorId = state.floorId
			}
            service.common.step.addVisitRecordZfb(params);
        },
		addVisitRecordUnAuthorized({ commit }, state) {
			let params = {
				pageType: state.pageType
			}
			if (state.floorId) {
				params.floorId = state.floorId
			}
			if (uni.getStorageSync('userId')) {
				params.userId = uni.getStorageSync('userId');
			}
		    service.million.wechatApplet.addVisitRecordUnAuthorized(params).then(res => {
				if (!uni.getStorageSync('userId')) {
					uni.setStorageSync('userId', res.userId)
				}
			});
		}
    },

    mutations: {
        SET_USERINFO(state, value) {
            state.info = value;
        },

        SET_CONF(state, value) {
            state.conf = value;
        },

        SET_TOKEN(state, value) {
            state.token = value;
            uni.setStorageSync('token', value);
        },

        CLEAR_TOKEN(state) {
            state.token = null;
            uni.removeStorageSync('token');
        },

        SET_APP(state, value) {
            state.app = value;
            uni.setStorageSync('app', value);
        },

        SET_SDKTYPE(state, value) {
            state.sdkType = value;
            uni.setStorageSync('sdkType', value);
        },

        SET_BACK(state, value) {
            state.back = value;
            uni.setStorageSync('back', value);
        }
    }
};
